﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSGS06
    {
        #region ==GSART===========
        public static readonly DependencyProperty GSARTProperty = DependencyProperty.Register("GSART",
            typeof(string), typeof(TSGS06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSARTChanged));
        public string GSART
        {
            get
            {
                return (string)GetValue(GSARTProperty);
            }
            set
            {
                SetValue(GSARTProperty, value); 
                Load();
            }
        }
        private static void OnGSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSGS06;
            if (src != null)
            {
                src.GSART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GSCFARBE===========
        public static readonly DependencyProperty GSCFARBEProperty = DependencyProperty.Register("GSCFARBE",
            typeof(string), typeof(TSGS06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSCFARBEChanged));
        public string GSCFARBE
        {
            get
            {
                return (string)GetValue(GSCFARBEProperty);
            }
            set
            {
                SetValue(GSCFARBEProperty, value);
                Locate();
            }
        }
        private static void OnGSCFARBEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSGS06;
            if (src != null)
            {
                src.GSCFARBE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==KEY===========
        public static readonly DependencyProperty KEYProperty = DependencyProperty.Register("KEY",
            typeof(string), typeof(TSGS06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKEYChanged));
        public string KEY
        {
            get
            {
                return (string)GetValue(KEYProperty);
            }
            set
            {
                SetValue(KEYProperty, value); 
            }
        }
        private static void OnKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSGS06;
            if (src != null)
            {
                src.KEY = (string)e.NewValue;
            }
        }
        #endregion 

        #region ==dwGS===========
        public static readonly DependencyProperty dwGSProperty = DependencyProperty.Register("dwGS",
            typeof(DataRow), typeof(TSGS06),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGSChanged));
        public DataRow dwGS
        {
            get
            {
                return (DataRow)GetValue(dwGSProperty);
            }
            set
            {
                SetValue(dwGSProperty, value); 
            }
        }
        private static void OndwGSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSGS06;
            if (src != null)
            {
                src.dwGS = (DataRow)e.NewValue;
            }
        }
        #endregion
         
        public TSGS06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGSCOLOR();
            DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGS = dwMaster;
            if (dwGS != null)
            {
                KEY = dwGS["GSCFARBE"].ToString();
                
            }
            else
            {
                KEY = "";
            }
        }
         
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public void Load()
        {
            if (GSART.Length < 23)
            {
                TBMain.Clear();
                return;
            }
            dwData = TBMain.NewRow();
            dwData["GSCMANDANT"] = app.Mandant;
            dwData["GSCART"] = GSART.Substring(1,20); 
            dwData["GSCVAR"] = GSART.Substring(21,3);  
            SResult = ((VGSCOLOR)MV).QueryGSCOLOR(dwData, -1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["DES"] = Helpers.Helper.GetGB2312Value(row["DES"].ToString());
            }
            MergeData(SResult, false);
        }

        public void Locate()
        {
            if (TBMain == null)
                return;
            if (TBMain.Rows.Count == 0)
                return;
            int idx = -1;
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["GSCFARBE"].ToString() == GSCFARBE
                        select row;
            foreach (DataRow row in query)
            {
                idx = TBMain.Rows.IndexOf(row);
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                Load();
            }
            else
            {
                VICC_WFEVENTS.SVWF.GS06(this.RUNID);
            }
            base.EditCommand(sender, e);
        }

    }
}
